package com.work.shart.common.mybatis.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.work.shart.common.mybatis.entity.PageParams;
import com.work.shart.common.mybatis.service.SuperService;
import com.work.shart.common.result.Result;
import com.work.shart.common.result.ResultVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;


/**
 * 外部查询接口
 * @param <PageDTO>
 * @param <Entity>
 */
public interface QueryController<PageDTO, Entity> extends SuperService<Entity> {
    @ApiOperation(value = "分页")
    @PostMapping("/page")
    default ResultVo page(@Validated @RequestBody PageParams<PageDTO> pageParams) {
        IPage<Entity> page = pageParams.buildPage(getEntityClass());
        IPage<Entity> iPage = getBaseService().page(page, pageHandelWrapper(BeanUtil.toBean(pageParams.getModel(), getEntityClass())));
        return Result.success(iPage);
    }

    @ApiOperation(value = "列表")
    @GetMapping("/list")
    default ResultVo list(PageDTO dto) {
        return Result.success(getBaseService().list(listHandelWrapper(BeanUtil.toBean(dto, getEntityClass()))));
    }

    @ApiOperation(value = "详情")
    @GetMapping("/{id}")
    default ResultVo get(@PathVariable(value = "id") Long id) {
        return Result.success(getBaseService().getById(id));
    }

    /**
     * 分页默认查询规则
     *
     * @param entity
     * @return
     */

    default Wrapper<Entity> pageHandelWrapper(Entity entity) {
        return new LambdaQueryWrapper(entity);
    }

    /**
     * 列表默认查询规则
     *
     * @param entity
     * @return
     */
    default Wrapper<Entity> listHandelWrapper(Entity entity) {
        return new LambdaQueryWrapper<>(entity);
    }


}
